package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SoshipheadMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smvehicle;
import com.unlcn.ils.erpInterfaces.dao.model.Soshiphead;
import com.unlcn.ils.erpInterfaces.dao.model.bo.NewestShipBO;
import com.unlcn.ils.erpInterfaces.dao.model.bo.ShipLineInfoBo;
import com.unlcn.ils.erpInterfaces.dao.model.bo.ShippingInfoBo;
import com.unlcn.ils.erpInterfaces.service.buss.ISmvehicleService;
import com.unlcn.ils.erpInterfaces.service.buss.ISoshipheadService;
import com.unlcn.ils.erpInterfaces.service.util.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by lenovo on 2018/6/13.
 */
@Service
public class SoshipheadServiceImpl extends ServiceImpl<SoshipheadMapper, Soshiphead> implements ISoshipheadService {

    @Autowired
    private SoshipheadMapper soshipheadMapper;

    @Autowired
    private ISmvehicleService smvehicleService;

    @Override
    public ShippingInfoBo getNewestShip(String vclicense) {

        if (StringUtils.isEmpty(vclicense))
            return null;

        // 根据车牌号查找车辆 id
        Wrapper<Smvehicle> smvehicleWrapper = new EntityWrapper<>();
        smvehicleWrapper.eq("vclicense", vclicense);
        List<Smvehicle> smvehicleList = smvehicleService.selectList(smvehicleWrapper);
        if (CollectionUtils.isEmpty(smvehicleList))
            throw new BaseException("车牌号无效，若确认车牌号无误，请联系 erp 管理员维护");
        Smvehicle smvehicle = smvehicleList.get(0);

        // 获取车辆最新发运指令
        NewestShipBO newestShipBO = soshipheadMapper.getNewestShipByVehicleid(smvehicle.getIlineid());
        if (newestShipBO == null) {
            return null;
        }

        ShippingInfoBo ship = soshipheadMapper.getNewestShip(newestShipBO.getIlastShipid());
        //防止运力平台抓取到以前ERP已完成的车辆指令生成运单.  调度时间超过1周或者指令明细最小值状态>=5的不返回
        if (ship != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -7);
            if (ship.getDtdate().after(calendar.getTime())) {
                ship.setCheckinId(newestShipBO.getCheckinId());
                return ship;
            }
        }
        return null;
    }

    @Override
    public List<ShippingInfoBo> getAllShipByLicense(String vclicense, String startTime, String endTime) {

        if (StringUtils.isEmpty(vclicense))
            throw new BaseException("车牌号为空");

        if (StringUtils.isEmpty(startTime))
            throw new BaseException("起始时间为空");

        if (StringUtils.isEmpty(endTime))
            throw new BaseException("截至时间为空");

        return soshipheadMapper.getAllShipByLicense(vclicense, startTime, endTime);
    }

    /**
     * 根据调度指令号查找调度指令
     */
    @Override
    public Soshiphead getSoshipheadByShipNo(String shipNo) {
        EntityWrapper<Soshiphead> shipEW = new EntityWrapper<>();
        shipEW.eq("vcshipno", shipNo);
        return selectOne(shipEW);
    }
}
